import {get, del, request} from '@request'
import {ResType} from "st-common-req"
import {MenuType} from "../types/menu-type.ts"
import {MenuIconTypeEnum} from "../enums/menu-icon-type-enum.ts"

const API_PRE = '/menu'

/**
 * 获取菜单的完整信息
 */
export const getMenus = async (
  {
    id,
    name,
    type,
    pagePath,
    routeName,
    routePath,
    disabled,
    hidden,
    cached,
    labeled,
    isTree = true,
  }: {
    id?: number;
    name?: string;
    type?: MenuIconTypeEnum;
    pagePath?: string;
    routeName?: string;
    routePath?: string;
    disabled?: boolean;
    hidden?: boolean;
    cached?: boolean;
    labeled?: boolean;
    isTree?: boolean;
  } = {}
) => {
  return await get(
    API_PRE,
    {
      id: id,
      name: name,
      type: type,
      pagePath: pagePath,
      routeName: routeName,
      routePath: routePath,
      disabled: disabled,
      hidden: hidden,
      cached: cached,
      labeled: labeled,
      isTree: isTree,
    }
  )
}

/**
 * 保存菜单信息
 *
 * @param {Menu} menu 菜单信息
 */
export const saveMenu = async (menu: MenuType): Promise<ResType> => {
  return await request.post(
    API_PRE,
    menu
  )
}

/**
 * 删除菜单
 *
 * @param {number[]} id 需要删除的菜单的 id 组成的列表
 * @param {boolean} isCascade 是否级联删除，即删除要删除菜单下面的子菜单，默认 false
 * @returns {Promise<ResType>} 删除结果
 */
export const delMenus = async (id: number[], isCascade: boolean = false): Promise<ResType> => {
  return await del(
    API_PRE,
    {
      id,
      isCascade
    }
  )
}
